package week5;

public class Product implements Comparable<Product> {
    private String name;
    private double price;

    public Product(String name, double price)
    {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }

    public boolean equals(Object other)
    {
        boolean result = false;
        if (other instanceof Product)
        {
            Product otherProduct = (Product) other;
            if (name.equals(otherProduct.getName()) && price == otherProduct.getPrice())
            {
                result = true;
            }
        }
        return result;
    }

    public String toString()
    {
        String result = "商品名：" + name + " 价格：" + price;
        return result;
    }

    @Override
    public int compareTo(Product product) {
        if (price > product.getPrice()) {
            return 1;
        } else {
            if (price < product.getPrice()) {
                return -1;
            } else {
                if (name.compareTo(product.getName()) > 0) {
                    return 1;
                } else {
                    if (name.compareTo(product.getName()) < 0) {
                        return -1;
                    } else {
                        return 0;
                    }
                }
            }
        }
    }
}
